package com.alorma.bcn;

import greendroid.app.GDActivity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.ListView;

import com.alorma.bcn.objects.ConsejoEntity;
import com.alorma.bcn.objects.ConsejosAdapter;
import com.android.dataframework.DataFramework;
import com.android.dataframework.Entity;

public class VerMasActivity extends GDActivity{

	private String CODI = "";
	private String TABLE_A = "alerta";
	private String TABLE_C = "consejos";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        
        try
        {
        	DataFramework.getInstance().open(this, "com.alorma.bcn");
        } catch (Exception e){
        	e.printStackTrace();
        }
        
		setActionBarContentView(R.layout.vermas);
		setTitle("Consells");
		
		ListView lista = (ListView) findViewById(R.id.listc);
		
		Bundle extras = getIntent().getExtras();
        if (extras.containsKey("CODIGO"))
        {
        	CODI = extras.getString("CODIGO");
        }
        
        String where = "codi = \"" + CODI + "\"";
        List<Entity> entidad = DataFramework.getInstance().getEntityList(TABLE_A, where);
        
        String ids[] = entidad.get(0).getString("cons").split("-");
        
        ArrayList<ConsejoEntity> consejos = new ArrayList<ConsejoEntity>();
        
        for (int i = 0; i <ids.length; i++)
        {
        	Entity ent = new Entity(TABLE_C, Long.parseLong(ids[i]));
        	consejos.add(new ConsejoEntity().convertFromEntity(ent));
        }
        
        ConsejosAdapter adapter = new ConsejosAdapter(this, android.R.id.text1, consejos);
        
        lista.setAdapter(adapter);
	}
	
    @Override
    protected void onDestroy() {
        try
        {
        	DataFramework.getInstance().close();
        } catch (Exception e){
        	e.printStackTrace();
        }
    	super.onDestroy();
    }
}
